import os

from flask_restplus import Resource, Namespace, fields, marshal_with
from marshmallow import INCLUDE

from common.utils.decorators import login_required
from flask import g, request, jsonify, Response
from app.models import db
from app import redis_clent, app
from flask_restplus import reqparse, inputs
from app.routes.data_collection.schemas import DataCollectionSchemas
from app.tasks.test import test
from common.utils.backends import AliyunOssStorage
from app.models.collection import DataCollection, CollectionImage
from common.utils.image_tools import delete_image
from .marshmallow import (
    DataCollectionQuerySerializer,
    CollectionDataSerizlizer,
    CollectionPostSerizlizer,
    CollectionIdSerizlizer,
    CollectionPutSerizlizer,
    CollectionBodySerizlizer,
    CollectionImagePostQuerySerizlizer,
    CollectionImageIdSerizlizer,
)

data_collection_ns = Namespace("data_collection")
data_collection_schema = DataCollectionSchemas(namespace=data_collection_ns)

ali_oss = AliyunOssStorage()


class CollectionImageView(Resource):
    @data_collection_ns.expect(data_collection_schema.CollectionImageCreateSchemas())
    @data_collection_ns.marshal_with(data_collection_schema.CollectionImageIdSchemas())
    def post(self):
        image_query = CollectionImagePostQuerySerizlizer()
        image_body = CollectionImageIdSerizlizer()

        image_query.context["request"] = request

        image = image_query.load(data=request.form)
        response = image_body.dump(image)
        return response


class CollectionImageDeleteView(Resource):
    def delete(self, id):

        try:
            image = CollectionImage.query.get(id)
        except Exception as e:
            raise e
        delete_image(image.file)
        db.session.delete(image)
        # ali_oss.delete(image.file)
        db.session.commit()

        return Response(status=204 if image else 500)


class DataCollectionChangeView(Resource):
    method_decorators = [login_required]

    def delete(self, id):
        try:
            collection = DataCollection.query.get(id)
        except Exception as e:
            raise e
        collection.deleted = True
        db.session.commit()

        return Response(status=204)

    @data_collection_ns.expect(data_collection_schema.DataCollectionPostSchemas())
    def put(self, id):

        request.json["id"] = id
        collection = CollectionPutSerizlizer().load(request.json)

        return Response(status=202 if collection == 1 else 500)

    @data_collection_ns.marshal_with(data_collection_schema.DataCollectionInfoSchemas())
    def get(self, id):
        # 查询记录详情
        try:
            collection = DataCollection.query.filter_by(id=id).first()
        except Exception as e:
            raise e

        response = CollectionBodySerizlizer().dump(collection)

        return response


class DataCollectionListView(Resource):

    method_decorators = [login_required]  # 检查用户登录的装饰器

    @data_collection_ns.marshal_with(data_collection_schema.DataCollectionBodySchemas())  # 指定序列化返回
    @data_collection_ns.doc(
        params=data_collection_schema.DataCollectionListQuerySchemas()
    )  # 指定输入字段
    def get(self):

        data, total = DataCollectionQuerySerializer().load(data=request.args)
        response = CollectionDataSerizlizer().dump({"data": data, "total": total})

        return response

    @data_collection_ns.expect(data_collection_schema.DataCollectionPostSchemas())
    @data_collection_ns.marshal_with(data_collection_schema.DataCollectionIdSchemas())
    def post(self):
        """创建记录"""
        # data = hello_ns.payload

        collection = CollectionPostSerizlizer().load(data=request.json)
        response = CollectionIdSerizlizer().dump(collection)
        return response
